package com.iwdnb.gkgz.infrastructure.dao.repo.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.iwdnb.bmnf.common.exception.ExceptionsEnhancer;
import com.iwdnb.gkgz.infrastructure.dao.entity.StockDayDataDO;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockDayDataRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK_DAY;

@Component
public class StockDayDataRepositoryImpl implements StockDayDataRepository {
    @Autowired
    private MetaDataDAO metaDataDAO;

    @Override
    public StockDayDataDO queryDate(String code, String date) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        metaQuery.addParam("date", date);
        return metaDataDAO.get(OBJECT_CODE_STOCK_DAY, metaQuery, StockDayDataDO.class);
    }

    @Override
    public List<StockDayDataDO> queryAllByDateAfter(String code, String date) {
        ExceptionsEnhancer.createError("not support");
        return null;
    }

    @Override
    public List<StockDayDataDO> queryAllByDateRange(String code, String startDate, String endDate) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        metaQuery.addParam("dateBegin", startDate);
        metaQuery.addParam("dateEnd", endDate);

        return metaDataDAO.queryListByCondition(OBJECT_CODE_STOCK_DAY, metaQuery, StockDayDataDO.class);
    }

    @Override
    public StockDayDataDO queryDayBeforeData(String code, String endDate) {
        ExceptionsEnhancer.createError("not support");
        return null;
    }

    @Override
    public List<StockDayDataDO> queryAllByYear(String code, String year) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        metaQuery.addParam("year", year);
        return metaDataDAO.queryListByCondition(OBJECT_CODE_STOCK_DAY, metaQuery, StockDayDataDO.class);
    }

    @Override
    public void save(StockDayDataDO stockDayDataDO) {
        if (Objects.isNull(stockDayDataDO.getId())) {
            metaDataDAO.insert(OBJECT_CODE_STOCK_DAY, stockDayDataDO);
        } else {
            metaDataDAO.update(OBJECT_CODE_STOCK_DAY, stockDayDataDO);
        }
    }

    @Override
    public void deleteByDateAfterAndDateBefore(Date startDate, Date endDate) {
        ExceptionsEnhancer.createError("not support");
    }

    @Override
    public void saveAll(List<StockDayDataDO> stockDayDataDOList) {
        for (StockDayDataDO stockDayDataDO : stockDayDataDOList) {
            save(stockDayDataDO);
        }
    }

}
